<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['setting']=$this->plugin_setting('basic');
$data=array('1','3','5');
if($_RQ['op']=='main'){
	$logid=intval($_RQ['id']);
	if(!$logid){
		exi('请选择订单！','error');
	}
	$logtype=intval($_RQ['type']);
	$params2=array('uniacid'=>$_SESSION['uniacid']);
	if(!$logtype){
		$table='jy_farmer_gdopt';
		$table2='jy_farmer_guoshu';
	}elseif($logtype==1){
		$table='jy_farmer_plant';
	}elseif($logtype==2){
		$table='jy_farmer_adopt';
	}
	$sendtype=$return['selectstore']?0:1;
	$log=pdo_get($table,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
	if(!$logtype){
		$params2['id']=$log['guoshu_id'];
	}elseif($logtype==1){
		$params2['id']=$log['guoshu_id'];
	}elseif($logtype==2){
		$params2['id']=$log['guoshu_id'];
	}
	$animal=pdo_get($table2,$params2);	
	if($animal['down_send_type1']!=1 && $return['setting']['down_send_type1']!=1){
		$return['sendtypes'][1]='快递配送';
		$sendtype=1;
	}
	if($animal['down_send_type0']!=1 && $return['setting']['down_send_type0']!=1){
		$return['sendtypes'][0]='店内自提';
		$sendtype=0;
	}
	$return['order']=pdo_get('jy_farmer_order',array('id'=>$log['order_id'],'uniacid'=>$_SESSION['uniacid']));
	$return['animal']=$animal;
	$return['log']=$log;
	$return['selectstore']=$return['setting']['stores'][0]?:array();
	$post=array('orderid'=>$log['order_id'],'logid'=>$logid,'logtype'=>$logtype,'sendtype'=>$sendtype,'total'=>1,'address'=>array('pcd'=>array('city'=>'深圳市')),'peisongfee'=>$return['setting']['peisongfee']);
	//刷新运费
	$up_dispatch=$this->model('dispatch')->up_dispatch($post,'',$log['shoukg']);
	$post=$up_dispatch['post']?:$post;
	$post['peisongfee']=$post['dispatchprice']?:$post['peisongfee'];
	$address=pdo_get('core_address',array('isdel'=>0,'isdefault'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$address){
		$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$post['address']=$address;
	$post['addressid']=$address['id'];
	$return['post']=$post;
	$return['address']=$post['address'];
	$order=pdo_get('jy_farmer_order',array('mobile <>'=>'','mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('username','mobile'),'id desc');
	$return['post']['username']=$order['username'];
	$return['post']['mobile']=$order['mobile'];
	if($post['sendtype']==1){
		$return['post']['endprice']=$post['peisongfee'];
	}else{
		$return['post']['endprice']=0;
	}
	$return['province']=array();
	set_html('title','收获页面');
	exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	if($post){
		$order=pdo_get('jy_farmer_order',array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));
		if($order['status']!=1){
			exi('该订单的状态，不支持当前操作！','error');	
		}
			if($post['sendtype']!=1 && empty($post['username'])){
				exi('请输入姓名','error');
			}
			if($post['sendtype']!=1 && empty($post['mobile'])){
				exi('请输入联系电话','error');
			}
			if($post['sendtype']!=1 && !preg_match("/^1[345789]\d{9}$/ims",$post['mobile'])){  	 
				exi('请输入正确的手机号码','error');
			}
			if($post['sendtype']==1 && (!$post['address']['pcd']['province'] || !$post['address'])){
				exi('请填收货地址！','error');
			}
			$update=array(		
				'username'=>$post['username']?:$post['address']['name'],
				'mobile'=>$post['mobile']?:$post['address']['mobile'],
				'address'=>$post['address']['pcd']['province'].' '.$post['address']['pcd']['city'].' '.$post['address']['pcd']['district'].' '.$post['address']['pcd']['street'].' '.$post['address']['address'],
				'peisongfee'=>$post['sendtype']==1?$post['peisongfee']:0,
				'sendtype'=>$post['sendtype'],
				'getstore'=>$post['sendtype']==1?'':ijson_encode($return['setting']['stores'][$post['storekey']]),
			);
			pdo_update('jy_farmer_order',$update,array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));
			if($update['peisongfee']>0){
				$params['fee']=$update['peisongfee'];
				$params['title']='配送费';
				$params['id']=$post['orderid'];
				$params['plugin']='jy_farmer';
				$params['back_url']='jy_farmer/shou2/payresult';
				$payid=cfc('pay')->create_pay_order($params);
				$this->pdo_update('order',array('ppay_id'=>$payid),array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));
				$res=array(
					'pay_id'=>$payid,
				);
				exi($res);
			}else{
				$this->pdo_update('order',array('status'=>6),array('id'=>$post['orderid'],'uniacid'=>$_SESSION['uniacid']));
				exi('提交成功！','','goback');
			}
			
	}else{
	   exi('请完善购买信息','error');
   }
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_farmer_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['peisongfee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_farmer_order',array('status'=>6),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	if($order['guoshu_id']){
		$table='jy_farmer_gdopt';
	}elseif($order['seed_id']){
		$table='jy_farmer_plant';
	}elseif($order['animal_id']){
		$table='jy_farmer_adopt';
	}
	pdo_update($table,array('status'=>5,'shoutype'=>$order['sendtype']),array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order['id']));
	exi('支付成功');
}